package com.yonyou.portcity.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * HTTP 返回结果
 *
 * @param <T> responseBody泛型
 */
public class WebResult<T> {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    private static final int SUCCESS_STATUS = 200;
    private static final String SUCCESS_MESSAGE = "请求成功";
    private static final int ERROR_STATUS = 500;
    private static final String ERROR_MESSAGE = "未知异常，请联系管理员";

    private String timestamp;

    private Integer status;

    private String error;

    private String message;

    private String path;

    private T data;

    public static <T> WebResult<T> success(String message, T data) {
        WebResult<T> webResult = new WebResult<>();
        webResult.setStatus(SUCCESS_STATUS);
        webResult.setMessage(message);
        webResult.setData(data);
        webResult.setTimestamp(getCurrentDate());
        return webResult;
    }

    public static <T> WebResult<T> success(String message) {
        return success(message, null);
    }

    public static <T> WebResult<T> success(T data) {
        return success(SUCCESS_MESSAGE, data);
    }

    public static <T> WebResult<T> success() {
        return success(SUCCESS_MESSAGE, null);
    }

    public static <T> WebResult<T> error(Integer status, String error, String message, T data) {
        WebResult<T> webResult = new WebResult<>();
        webResult.setStatus(status);
        webResult.setError(error);
        webResult.setMessage(message);
        webResult.setData(data);
        webResult.setTimestamp(getCurrentDate());
        return webResult;
    }

    public static <T> WebResult<T> error(Integer status, String error, String message) {
        return error(status, error, message, null);
    }

    public static <T> WebResult<T> error(Integer status, String error) {
        return error(status, error, error, null);
    }

    public static <T> WebResult<T> error(String error, String message) {
        return error(ERROR_STATUS, error, message, null);
    }

    public static <T> WebResult<T> error(String error) {
        return error(ERROR_STATUS, error, error, null);
    }

    public static <T> WebResult<T> error() {
        return error(ERROR_STATUS, ERROR_MESSAGE, ERROR_MESSAGE, null);
    }

    private static String getCurrentDate() {
        return dateFormat.format(new Date());
    }

    public static SimpleDateFormat getDateFormat() {
        return dateFormat;
    }

    public static void setDateFormat(SimpleDateFormat dateFormat) {
        WebResult.dateFormat = dateFormat;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
